package com.example.xdemo.slice;

import com.example.xdemo.MyAdapter;
import com.example.xdemo.ResourceTable;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;

public class GridAbilitySlice extends AbilitySlice {
    private XRecyclerView mXRecyclerView;
    private MyAdapter<String> adapter;
    private ArrayList<String> listData;
    private int refreshTime = 0;

    private final EventHandler handler = new EventHandler(EventRunner.getMainEventRunner());

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_grid);
        initView();
    }

    private void initView(){
        mXRecyclerView = (XRecyclerView)findComponentById(ResourceTable.Id_abt_grid_xrecycleview);
        listData = new ArrayList<>();

        adapter = new MyAdapter<>(getContext(),listData,ResourceTable.Layout_list_item);

        adapter.setOnItemClickListener(new XRecyclerView.RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Component component, int position) {

                new ToastDialog(getContext()).setText("nihao"+position).show();
            }
        });

        for(int i = 0; i < 25; i++){
            listData.add("item -- "+i);
        }
        adapter.setNumColumns(2);
        mXRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mXRecyclerView.setLoadingMoreProgressIndicatorColor(Color.RED.getValue());
        mXRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                handler.removeTask(refreshRunnable);
                handler.postTask(refreshRunnable, 1000);
            }

            @Override
            public void onLoadMore() {
                handler.removeTask(loadMoreRunnable);
                handler.postTask(loadMoreRunnable, 1000);
            }
        });

        mXRecyclerView.setItemProvider(adapter);
    }

    private final Runnable refreshRunnable = new Runnable() {
        @Override
        public void run() {
            refreshTime ++;
            listData.clear();
            for(int i = 0; i < 25 ;i++){
                listData.add("item" + i + "after " + refreshTime + " times of refresh");
            }
            adapter.notifyDataChanged();
            mXRecyclerView.refreshComplete();
        }
    };

    private final Runnable loadMoreRunnable = new Runnable() {
        @Override
        public void run() {
            refreshTime ++;
            listData.clear();
            for(int i = 0; i < 25 ;i++){
                listData.add("item" + i + "after " + refreshTime + " times of refresh");
            }
            adapter.notifyDataChanged();
            mXRecyclerView.refreshComplete();
        }
    };
}
